package dbAccess;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import exceptions.DatabaseErrorException;
import exceptions.PasIdentificationException;

import model.OrderStatus;

public class OrderStatusDBAccess {

	public ArrayList<OrderStatus> getAllOrderStatus() throws PasIdentificationException, DatabaseErrorException {
		ArrayList<OrderStatus> ordStatList = new ArrayList<OrderStatus>();
		Connection conn = DBConnexionSingleton.getInstance();
		PreparedStatement stat;
		String query = "select * from order_status order by order_status_id;";
		try {
			stat = conn.prepareStatement(query);
			ResultSet data = stat.executeQuery();
			while(data.next()){
				ordStatList.add(new OrderStatus(data.getInt("order_status_id"), data.getString("order_status_verbose")));
			}
			stat.close();
		} catch (SQLException e) {
			throw new DatabaseErrorException(e);
		}
		return ordStatList;
	}
	public OrderStatus getOrderStatus(int orderStatusID) throws PasIdentificationException, DatabaseErrorException{
		OrderStatus ordStat = null;
		Connection conn = DBConnexionSingleton.getInstance();
		PreparedStatement stat;
		String query = "select order_status_verbose from order_status where order_status_id = ?;";
		try {
			stat = conn.prepareStatement(query);
			stat.setInt(1, orderStatusID);
			ResultSet data = stat.executeQuery();
			while(data.next()){
				ordStat = new OrderStatus(orderStatusID, data.getString("order_status_verbose"));
			}
			stat.close();
		} catch (SQLException e) {
			throw new DatabaseErrorException(e);
		}
		return ordStat;
	}
}
